
import * as React from 'react';
import { Button, View, Text, SafeAreaView, StatusBar } from 'react-native';
import {WebView} from 'react-native-webview';
import router from '../../util/router'
import storage from '../../util/storage';
import MWebview from '../../baseUI/h5/webview'
import { globalNotiInstance } from '../../notifiService'
import BSafeAreaViewPlus from '../../baseUI/b-safe-area-plus';
import {
  BackHandler,
  Platform,
} from 'react-native';

export default class HomeScreen extends MWebview {
  state = { token : '' };

  pushNoti = () => {
    globalNotiInstance.localNotif();
  }

  // webView = {
  //   canGoBack: false,
  //   ref: null,
  // }

  onAndroidBackPress = () => {
    // if (this.webView.canGoBack && this.webView.ref) {
    //   this.webView.ref.goBack();
    //   return true;
    // }
    this.injectJavaScript(`window.onPressBack()`)
    // injectJavaScript = (jsCode) => {
    //   const webView = this.refs['webview_ref'];
    //   webView.injectJavaScript(jsCode);
    // };
    return true;
  }

  componentWillMount() {
    if (Platform.OS === 'android') {
      BackHandler.addEventListener('hardwareBackPress', this.onAndroidBackPress);
    }
  }

  componentWillUnmount() {
    if (Platform.OS === 'android') {
      BackHandler.removeEventListener('hardwareBackPress');
    }
  }

  render() {
    const url = 'http://wui.airtlab.com';
    // const url = 'http://ddd.shunqian667.com';
    // const url = 'http://192.168.0.103:8080/'
    const runFirst = `
      window.REACT_NATIVE = true;
      window.LOGIN_TOKEN = "${this.state.token}";
      true; // note: this is required, or you'll sometimes get silent failures
    `;
    
    return (
      <BSafeAreaViewPlus topColor="#d6392f">
        <StatusBar backgroundColor="#d6392f"  barStyle="light-content" />
        <WebView
          ref={'webview_ref'}
          source={{
            uri: url,
            headers: { 'Cache-Control':'no-cache'}
          }}
          domStorageEnabled={true}
          injectedJavaScriptBeforeContentLoaded={runFirst}
          scrollEnabled={true}
          javaScriptEnabled={true}
          showsVerticalScrollIndicator={false}
          showsHorizontalScrollIndicator={false}
          onMessage={(event) => {
            this.handleMessage(event);
          }}
          onNavigationStateChange={(navState) => {
            console.log(navState)
            // this.webView.canGoBack = navState.canGoBack;
          }}
        />
      </BSafeAreaViewPlus>
    );
  }
}